'''
Created on 2011-11-21

@author: Administrator
'''
import urllib2
import datetime
class SINAStock():
    '''
    get A,HK,US stock list from all vendor
    '''

    def getstocklist(self,reg):
        if reg=='SH':return self.getSHstocklistfromSINA()
        elif reg=='HK':return self.getHKstocklistfromSINA()
        elif reg=='NS':return self.getUSstocklistfromSINA()
        elif reg=='SZ':return self.getSZstocklistfromSINA()
        else:print 'error reg'
    def getstocklist2(self,reg):
        if reg=='CH':return self.getCNstocklist()
        elif reg=='HK':return self.getHKstocklist()
        elif reg=='US':return self.getUSstocklist()
        else:print 'error reg'
    def getusstocklistfromimeigu(self):
        def getonepage(page):
            url='http://hq.imeigu.com/list.jsp?od=0&ac=0&&pagex='+str(page)
            content=urllib2.urlopen(url).read().decode('utf8')
            s=content.find('<table cellspacing="0">')
            p=content.find('</table',s)
            nlist=content[s:p].split('<tr')
            stocklist={}
            for n in nlist:
    #            print n
                tlist=n.split('<td')
                onestock=[]
                tstock=[]
                for t in tlist:
                    p=t.find('</td')
                    k=t.rfind('</a',0,p)
                    if k>0:p=k
                    s=t.rfind('>',0,p)
                    v=t[s:p].strip(' >\t\n')
                    tstock.append(v)
    #                print '[',v,']',len(v)
                    if len(tstock)>4:
                        onestock.append(tstock[1])
                        onestock.append(tstock[2])
                
            stocklist[onestock[1]]=onestock[2]
            return len(nlist)
        page=1
        ret=getonepage(page)
        print ret
    #    while ret==30:
    #        page=1+page
    #        ret=getonepage(page)         
        return []
    def getHKstocklistfrombloomberg(self):
        def parseonestock(src):
            nlist=src.split('<td')
            ret=[]
            for n in nlist:
                
                p=n.find('</td')
                k=n.rfind('</span',0,p)
                if k>0:p=k
                s=n.rfind('>',0,p)
                v=n[s:p].strip('>\n\t ')
                if len(v)>0: ret.append(v)
            return ret
            pass
        def getonepage(page=1):
            url='http://www.bloomberg.com/markets/companies/country/usa/'+str(page)+'/'
    #        url='http://www.bloomberg.com/markets/companies/country/hong-kong/'+str(page)+'/'
    #        url='http://www.bloomberg.com/markets/companies/country/china/'+str(page)+'/'
            content=urllib2.urlopen(url).read()
            s=content.find('<table class="ticker_data">')
            p=content.find('</table',s)
            nlist=content[s:p].split('<tr')
            stocklist=[]
            for n in nlist:
                ret=parseonestock(n)
                if len(ret)>0:stocklist.append(ret)
            return stocklist
        stocklist=[]
        tlist=[]
        page=1
        tlist=getonepage(page)
        print "page:",page," tlist len:",len(tlist)
        if len(tlist)==0:return 0
        else:
            for t in tlist:
                stocklist.append(t)
            while len(tlist)>0:
                page=page+1
                tlist=getonepage(page)
                print "page:",page," tlist len:",len(tlist)
                for t in tlist:
                    stocklist.append(t)           
        return stocklist

    def getHKstocklistfromSINA(self):
        url='http://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getHKStockData?page=1&num=8000&sort=symbol&asc=1&node=qbgg_hk&_s_r_a=page'
#        print url
        nlist=urllib2.urlopen(url).read().decode('gb2312').strip('[]{}').split('},{')  
#        print len(nlist),nlist[0]
        stocklist={}  
        for n in nlist:
            tlist=n.split(',')
            rlist={}
            for t in tlist:
                plist=t.split(':')
                rlist[plist[0]]=plist[1].strip('"')
            stocklist[rlist['symbol']]=rlist['name']
#            print rlist
#            break
        return stocklist
    def getUSstocklistfromSINA(self):
        nodelist=['china_us','yysp_us','meida_us','auto_us','sales_us','finance_us','tech_us']
        urlpre='http://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getUSList?page=1&num=8000&sort=chg&asc=0&_s_r_a=init&node='
        stocklist={}
        for node in nodelist:
            url=urlpre+node
            nlist=urllib2.urlopen(url).read().strip('{}[]').split('},{')
#            print node,len(nlist)
            for n in nlist:
                tlist=n.split(':')
                code=tlist[1].strip('"')
                stocklist[code]=code
        s2list=self.getUSstocklistfromSINA2()
        for s2 in s2list.keys():
            stocklist[s2]=s2
        return stocklist
    def getUSstocklistfromSINA2(self):
        url='http://vip.stock.finance.sina.com.cn/usstock/ustotal.php'
        stocklist={}
        nlist=urllib2.urlopen(url).read().split('<div class="col_div">')
        nlist.pop(0)
        for n in nlist:
            i=n.find('</div')
            clist=n[:i].split('US100_')
            clist.pop(0)
            for c in clist:
                k=c.find('.')
                stocklist[c[:k]]=c[:k]
        return stocklist

    def getSHstocklistfromSINA(self):
        url='http://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getHQNodeData?page=1&num=20000&sort=symbol&asc=1&node=sh_a&_s_r_a=init'
        content=urllib2.urlopen(url).read().decode('gb2312')
#        print content
        clist=content.strip(' []{}').split('},{')
#        print len(clist)
        stocklist={}
        for c in clist:
            flist=c.split(',')
            fsone={}
            for f in flist:
                tlist=f.split(':')
                fsone[tlist[0]]=tlist[1].strip('"')
            stocklist[fsone['code']]=fsone['name']
        return stocklist
    def getSZstocklistfromSINA(self):
        url='http://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getHQNodeData?page=1&num=20000&sort=symbol&asc=1&node=sz_a&_s_r_a=init'
        content=urllib2.urlopen(url).read().decode('gb2312')
#        print content
        clist=content.strip(' []{}').split('},{')
#        print len(clist)
        stocklist={}
        for c in clist:
            flist=c.split(',')
            fsone={}
            for f in flist:
                tlist=f.split(':')
                fsone[tlist[0]]=tlist[1].strip('"')
            stocklist[fsone['code']]=fsone['name']
        return stocklist
if __name__ == '__main__':
    pass